<template>
	<view>
		<nav-bar   fontColor="#FFFFFF" title="变更货架"></nav-bar>
		<!-- <view v-for="item in getList" class="content">
			<view class="contentFont">案件编号：{{item.no}}</view>
			<view class="contentFont">案件名称：{{item.name}}</view>
			<view class="contentFont">借用人员：{{item.person}}</view>
			<view class="contentFont">借用状态：{{item.status}}</view>
			<view class="contentFont">借用时间：{{item.time}}</view>
		</view> -->
		<view v-if="rfidStatus & !kuweiStatus">
			<view class="orderNumber">数量：{{textData.length}}</view>
			<view class="imgCode">
				<image class="qrImg" src="../../static/icon/scanning_ico.png"></image>
				<view class="qrTitle">请扫描RFID射频标签</view>
			</view>
			<view class="buttonBox">
				<button class="button_fix" @click="goNext" size="default">下一步</button>
			</view>
		</view>
		<view v-else-if="!rfidStatus & kuweiStatus">
			<view class="orderNumber center txtTop">库位号：{{yuanQrCode}}</view>
			<view @click="goQrCode1" class="imgCode">
				<image class="qrImg" src="../../static/icon/scan.png"></image>
				<view class="qrTitleBlue">请扫描原库位二维码标签</view>
			</view>
			<view class="buttonBox">
				<button class="button_fix" @click="backRfid" size="default">返回</button>
				<button class="button_fix" @click="goyuanNext" size="default">下一步</button>
			</view>
		</view>
		<view v-else>
			<view class="orderNumber center txtTop">库位号：{{nowQrCode}}</view>
			<view @click="goQrCode2" class="imgCode">
				<image class="qrImg" src="../../static/icon/scan.png"></image>
				<view class="qrTitleBlue">请扫描现库位二维码标签</view>
			</view>
			<view class="buttonBox">
				<button class="button_fix" @click="goYuanQrCode" size="default">返回</button>
				<button class="button_fix" @click="upLoadData" size="default">提交</button>
			</view>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="关闭" confirmText="确认" title="通知" content="xxx"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog :deleteIcon="true" :dataList="textData" title="确认信息" ref="inputClose" @confirm="confirm"
				mode="scroll">
			</uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="3000"></uni-popup-message>
		</uni-popup>
		<!-- <image src="../../static/img/chahua3.png" class="bottomImg"></image> -->
		<!-- <view v-if="rfId && qrCode" @click="goRfid" style="width: 60%;height: 400rpx;border: 6rpx solid #000000;margin: 0 auto;margin-top: 300rpx;border-radius: 15rpx;border-color: #555555;">
			
			<htz-image-upload :max="1" mediaType="image" name="file" :chooseNum="1" v-model="imageData"
				@chooseSuccess="ceshiChooseSuccess" @imgDelete="deleteImg">
			</htz-image-upload>
			<view style="text-align: center;font-size: 30rpx;color: #808080;margin-top: 20rpx;">请上传图片</view>
		</view> -->
		<uni-popup ref="hintDialog" type="dialog">
			<uni-popup-dialog type="info" :beforeClose="true" cancelText="返回首页" confirmText="继续变更" title="提示" content="还有其他物品需要继续变更吗？"
				@confirm="hintConfirm" @close="hintClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	var main = null;
	var receiver = null;
	import htzImageUpload from '@/components/htz-image-upload/htz-image-upload-show.vue'
	export default {
		components: {
			htzImageUpload,
		},
		data() {
			return {
				rfidStatus: true,
				kuweiStatus: false,
				imageData: [],
				getList: [],
				rfId: [],
				repeactId: [],
				yuanQrCode: '',
				nowQrCode: '',
				popStatus: uni.getStorageSync('biangeng'),
				msgType: 'success',
				messageText: '这是一条成功提示',
				// textData: [{
				// 	itemName: '测试1',
				// 	caseName: '测试案件',
				// 	serialNo: 'W3101159200002020090035',
				// 	caseNo:'A1',
				// 	rfidCode: 'E280110C2000724B15730A8B'
				// },
				// {
				// 	itemName: '测试2',
				// 	caseName: '测试案件',
				// 	serialNo: 'W3101159200002020090035',
				// 	caseNo:'A2',
				// 	rfidCode: '123456'
				// }]
				textData: []
			}
		},
		onLoad() {
			console.log("onLoad");
			this.get_gb();
		},
		onReady() {
			// if (this.popStatus) {
			// 	this.dialogToggle('info')
			// }
		},
		onBackPress() {
			if (receiver != null) {
				main.unregisterReceiver(receiver)
			}
		},
		methods: {
			hintConfirm() {
				this.$refs.hintDialog.close();
			},
			hintClose() {
				this.$refs.hintDialog.close();
				uni.navigateBack({
					delta: 1
				});
			},
			backRfid() {
				var that = this;
				that.rfidStatus = !that.rfidStatus
				that.kuweiStatus = !that.kuweiStatus
				that.yuanQrCode = '';
			},
			goyuanNext() {
				var that = this;
				that.rfidStatus = false
				that.kuweiStatus = false
			},
			goYuanQrCode() {
				var that = this;
				that.rfidStatus = false
				that.kuweiStatus = true
				that.nowQrCode = ''
			},
			confirm() {
				var that = this;
				this.$refs.inputDialog.close();
				if (that.textData == '') {
					that.messageToggle('error', '请输入完整的数据！')
				} else {
					that.rfidStatus = !that.rfidStatus
					that.kuweiStatus = !that.kuweiStatus
				}
			},
			dialogConfirm() {
				uni.setStorageSync('biangeng', false)
				console.log('确认')
				this.$refs.alertDialog.close()
			},
			dialogToggle(type) {
				this.msgType = type
				this.$refs.alertDialog.open()
			},
			dialogClose() {
				console.log('取消')
				this.$refs.alertDialog.close()
			},
			get_gb() {
				var that = this;
				main = plus.android.runtimeMainActivity();
				receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						var data = intent.getExtra("data");
						if (data) {
							if (that.textData == '') {
								that.rfId = []
								that.repeactId = []
							}
							that.repeactId.push(data)
							for (let i = 0; i < that.repeactId.length; i++) {
								if (that.rfId.indexOf(that.repeactId[i]) == -1) {
									that.rfId.push(that.repeactId[i])
									that.pushArray(that.repeactId[i]);
								}
							}
						}
					}
				});
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				let filter = new IntentFilter();
				filter.addAction('android.rfid.epccode');
				var info = main.registerReceiver(receiver, filter)
				// main.unregisterReceiver(receiver)
			},
			pushArray(e) {
				var that = this;
				let opt = {
					url: 'oms/tbRelateItemTakeDetail/search',
					method: 'POST'
				};
				let params = {
					rfidCode: e
				};
				that.$http.httpTokenRequest(opt, params).then(res => {
					that.textData.push({
						itemName: res.data.data[0].itemName,
						caseName: res.data.data[0].caseName,
						serialNo: res.data.data[0].serialNo,
						caseNo: res.data.data[0].caseNo,
						rfidCode: e
					})
				})
			},
			goNext() {
				// this.$refs.message.open();
				if (this.textData == '') {
					this.messageToggle('error', '请扫描RFID射频标签');
				} else {
					this.$refs.inputDialog.open();
				}
			},
			goBack() {
				this.rfidStatus = !this.rfidStatus
				this.qrCode = '';
			},
			messageToggle(type, msg) {
				this.msgType = type
				this.messageText = msg
				this.$refs.message.open();
			},
			goQrCode1() {
				var that = this;
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let code1 = res.result.slice(0,2)
						let code2 = res.result.slice(2,4)
						let code3 = res.result.slice(4,6)
						let code4 = res.result.slice(6,8)
						that.yuanQrCode = code1 + '-' + code2 + '-' + code3 + '-' + code4
					}
				});
			},
			goQrCode2() {
				var that = this;
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						let code1 = res.result.slice(0,2)
						let code2 = res.result.slice(2,4)
						let code3 = res.result.slice(4,6)
						let code4 = res.result.slice(6,8)
						that.nowQrCode = code1 + '-' + code2 + '-' + code3 + '-' + code4
					}
				});
			},
			upLoadData() {
				console.log(this.textData)
				let codeRfid = []
				for (let i = 0; i < this.textData.length; i++) {
					codeRfid.push(this.textData[i].rfidCode)
				}
				console.log(codeRfid)
				let opt = {
					url: 'oms/tbRelateItemTakeDetail/changeStorageFromPda',
					method: 'POST'
				};
				let params = {
					rfidCodes: codeRfid,
					moveOutStoreLotNo: this.yuanQrCode,
					storeLotNo: this.nowQrCode
				};
				console.log(params)
				this.$http.httpTokenRequest(opt, params).then(res => {
					console.log(this.rfId)
					console.log(res)
					if (res.data.code == 200) {
						this.messageToggle('success', '变更成功')
						this.$refs.hintDialog.open();
						this.repeactId = [];
						this.rfId = [];
						this.textData = [];
						this.yuanQrCode = '';
						this.nowQrCode = '';
						this.rfidStatus = true;
						this.kuweiStatus = false;
					} else {
						this.messageToggle('error', res.data.msg)
						this.repeactId = [];
						this.rfId = [];
						this.textData = [];
						this.yuanQrCode = '';
						this.nowQrCode = '';
						this.rfidStatus = true;
						this.kuweiStatus = false;
						return
					}
				})
			},
			deleteImg(e) {
				console.log(e)
			},
			ceshiChooseSuccess(tempFilePaths, e) { //选择图片返回
				console.log('ceshiChooseSuccess', tempFilePaths, e);
				// this.imageData=this.imageData.concat(tempFilePaths)

				//this.imageData.push(...tempFilePaths)
				/****************
				以下代码是自定义上传逻辑，仅供参考
				***************/
				this.tempFilePaths = tempFilePaths
				// this.imageData.push(tempFilePaths[0]);
				this.imgUpload(tempFilePaths);
				console.log(tempFilePaths)
				/*******************************/
			},
			imgUpload(tempFilePaths) {
				if (!tempFilePaths) {
					uni.showToast({
						title: '请上传视频或者照片！',
						position: 'center',
						icon: 'error'
					});
					return
				}
				uni.showLoading({
					title: '上传中'
				});
				console.log('imgUpload', tempFilePaths)
				let uploadImgs = [];
				tempFilePaths.forEach((item, index) => {
					uploadImgs.push(new Promise((resolve, reject) => {
						console.log(index, item)
						uni.uploadFile({
							url: "http://183.246.198.66:29527/sys/file/upload", //仅为示例，非真实的接口地址
							filePath: item,
							// header:{
							// 	'Authorization':'Bearer ' + uni.getStorageSync('token')
							// },
							name: 'file',
							// formData: {
							// 	eventType: this.infoList.eventType, //事件类型
							// 	fireDegree: this.infoList.fireDegree, //火情等级
							// 	wormType: this.infoList.wormType, //病虫害类型
							// 	wormDegree: this.infoList.wormDegree, //病虫害等级
							// 	locationDesc: this.infoList.locationDesc + this.addressName, //地址
							// 	latitude: this.infoList.latitude, //经度
							// 	altitude: this.infoList.altitude //维度
							// },
							success: (uploadFileRes) => {
								console.log('uploadFileRes', uploadFileRes)
								resolve(uploadFileRes);
								var _res = JSON.parse(uploadFileRes.data);
								if (_res.code == 200) {
									console.log(_res.data)
									let str = _res.data
									let fileType = str.lastIndexOf(".")
									str = str.substring(fileType + 1, str.length);
									console.log(str)
									if (str == 'jpeg' || str == 'jpg' || str == 'png') {
										this.imageData.push(
											'http://183.246.198.66:29528/' + _res.data);
										this.fileStatus = true
										console.log('推送jpeg进数组')
									} else if (str == 'mp4') {
										// this.videoPath = 'http://115.29.199.1:81/' + _res.data
										this.imageData.push(
											'http://183.246.198.66:29528/' + _res.data);
										this.fileStatus = true
										console.log('推送mp4进数组')
										console.log(this.infoList.picCt)
									}
								}
							},
							fail: (err) => {
								console.log(err);
								reject(err);
							},
							complete: () => {
								//uni.hideLoading();
							}
						});
					}))
				})
				Promise.all(uploadImgs) //执行所有需请求的接口
					.then((results) => {
						uni.hideLoading();
					})
					.catch((res, object) => {
						uni.hideLoading();
					});
			}
		}
	}
</script>

<style>
	page {
		background: #fdfdfd;
	}

	.content {
		padding: 20rpx;
		letter-spacing: 2rpx;
		font-size: 35rpx;
		color: #646464;
		border-bottom: 1px solid #e2e2e2;
	}

	.imgCode {
		position: relative;
		background: linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) left top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right top,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) right bottom,
			linear-gradient(#000000, #000000) left bottom,
			linear-gradient(#000000, #000000) left bottom;
		background-repeat: no-repeat;
		background-size: 2px 20px, 20px 2px;
		height: 400rpx;
		width: 400rpx;
		margin: 0 auto;
		margin-top: 35%;
	}

	.center {
		text-align: center;
	}

	.txtTop {
		margin-top: 50rpx;
	}

	.qrImg {
		width: 250rpx;
		height: 250rpx;
		left: 20%;
		top: 15%;
	}

	.qrTitle {
		font-size: 28rpx;
		color: #808080;
		margin-top: 90rpx;
		text-align: center;
		letter-spacing: 3rpx;
	}

	.qrTitleBlue {
		font-size: 28rpx;
		color: #3B5DE0;
		margin-top: 90rpx;
		text-align: center;
		letter-spacing: 3rpx;
	}

	.contentFont {
		padding: 5rpx;
	}

	.orderNumber {
		padding: 30rpx;
		font-size: 35rpx;
		font-weight: 700;
		letter-spacing: 3rpx;
		text-align: center;
		/* margin-top: 50rpx; */
	}

	.buttonBox {
		width: 70%;
		margin: 0 auto;
		display: flex;
		margin-top: 200rpx;
	}

	.button_fix {
		width: 35%;
		height: 70rpx;
		margin: 0 auto;
		color: #FFFFFF;
		background: #2749C8;
		font-weight: bold;
		letter-spacing: 5rpx;
		opacity: 0.9;
		font-size: 28rpx;
		line-height: 70rpx;
	}

	.bottomImg {
		position: fixed;
		height: 350rpx;
		width: 250rpx;
		float: right;
		bottom: 0;
		left: 65%;
	}
</style>
